﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace SuperX.Helper
{
    public static class EnumToArrays
    {
        /// <summary>
        /// 枚举转集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumEntity> EnumToArray<T>() where T : Enum
        {
            Type type = typeof(T);
            var list = new List<EnumEntity>();
            foreach (var item in Enum.GetValues(type))
            {
                list.Add(new EnumEntity
                {
                    Key = item.ToString(),
                    Value = (int)item,
                    Name = (type.GetField(item.ToString()).GetCustomAttributes(true).FirstOrDefault() as DescriptionAttribute)?.Description ?? ""
                });
            }
            return list;
        }
    }

    public class EnumEntity
    {
        public string Key { get; set; }
        public int Value { get; set; }
        public string Name { get; set; }
    }
}